<?php
namespace Models;

require_once('User.php');
require_once('Token.php');
require_once('\Common\DBTables.php');

class UserCatalog
{	
	/**
	 * Hämtar en array av användare
	 * @return $users UserArray
	 */
	public function getUsers(\Models\Database $db)
	{
		$dbt = new \Common\DBTables();
		
		$sql = "SELECT ".
		$dbt->userIdField.",".
		$dbt->userTypeIdField.",".
		$dbt->userNameField.",".
		$dbt->firstNameField.",".
		$dbt->lastNameField." 
		FROM ".$dbt->userTable."";
		
		$statement = $db->Select($sql);
		
		// Bind resultat innan vi anropar fetch()
		if ($statement->bind_result($dbt->userIdField, $dbt->userTypeIdField, $dbt->userNameField, $dbt->firstNameField, $dbt->lastNameField) == FALSE)
			throw new \Exception($this->mysqli->error);

	    $users = new UserArray();
		
	    while ($statement->fetch())
	    {
	        $user = new \Models\User($dbt->userIdField, $dbt->userTypeIdField, $dbt->userNameField, $dbt->firstNameField, $dbt->lastNameField);
			$users->add($user);
	    }
		
		$statement->close();
		
		return $users;
	}
	
	/**
	 * Hämtar en användare
	 * @return $user User
	 */
	public static function getUser(\Models\Database $db, $userId)
	{
		$dbt = new \Common\DBTables();
		
		$sql = "SELECT ".
		$dbt->userIdField.",".
		$dbt->userTypeIdField.",".
		$dbt->userNameField.",".
		$dbt->firstNameField.",".
		$dbt->lastNameField." 
		FROM ".$dbt->userTable."
		WHERE ".$dbt->userIdField."='".$userId."'";
		
		$statement = $db->Select($sql);
		
		// Bind resultat innan vi anropar fetch()
		if ($statement->bind_result($dbt->userIdField, $dbt->userTypeIdField, $dbt->userNameField, $dbt->firstNameField, $dbt->lastNameField) == FALSE)
			throw new \Exception($this->mysqli->error);
		
	    if($statement->fetch())
	        $user = new \Models\User($dbt->userIdField, $dbt->userTypeIdField, $dbt->userNameField, $dbt->firstNameField, $dbt->lastNameField);
		else
			return null;
		
		$statement->close();
		
		return $user;
	}
	
	/**
	 * Hämtar en token
	 * @param $db Database
	 * @param $userId int
	 * @return $user User
	 */
	public static function getTokenFromUser(\Models\Database $db, $userId)
	{
		$dbt = new \Common\DBTables();
		
		$sql = "SELECT ".
		$dbt->tokenIdField.",".
		$dbt->userIdField.",".
		$dbt->tokenField.",".
		$dbt->ipField." 
		FROM ".$dbt->tokenTable."
		WHERE ".$dbt->userIdField."='".$userId."'";
		
		$statement = $db->Select($sql);
		
		// Bind resultat innan vi anropar fetch()
		if ($statement->bind_result($dbt->tokenIdField, $dbt->userIdField, $dbt->tokenField, $dbt->ipField) == FALSE)
			throw new \Exception($this->mysqli->error);
		
	    if($statement->fetch())
	        $token = new \Models\Token($dbt->tokenIdField, $dbt->userIdField, $dbt->tokenField, $dbt->ipField);
		else
			return null;
		
		$statement->close();
		
		return $token;
	}
	
	/**
	 * Hämtar en användare via en token
	 * @param $db Database
	 * @param $token Token
	 * @return User
	 */
	public static function getUserFromToken(\Models\Database $db, $token)
	{		
		$dbt = new \Common\DBTables();
		
		$sql = "SELECT ".
		$dbt->userIdField."
		FROM ".$dbt->tokenTable."
		WHERE ".$dbt->tokenField."='".$token."'";
		
		$statement = $db->Select($sql);
		
		// Bind resultat innan vi anropar fetch()
		if ($statement->bind_result($dbt->userIdField) == FALSE)
			throw new \Exception($this->mysqli->error);
		
	    if($statement->fetch())
			$userId = $dbt->userIdField;
		else
			return null;
		
		$statement->close();
		
		return self::getUser($db, $userId);
	}
}

?>